#!/usr/bin/env ruby -wKU
#encoding: UTF-8

require 'open-uri'
require 'observer'
#require 'thread'

require 'backend/model/Tieba'
require 'backend/crawler/HtmlParser'

module Crawler
  
  class TiebaCrawler
    
    include Observable
    
    def crawl
            
      tieba = Model::Tieba.instance
      threads = []
      
      tieba.novels.each do |novel|
        threads << Thread.new do
          retryCnt = 0
          while (true)
            begin
              chapters = HtmlParser.fetch(novel)
              
              unless chapters.nil?
                changed
                notify_observers(chapters)
              end
                        
              # sleep for 10 minutes
              sleep(10 * 60)
            rescue => ex
              $stderr.puts ex.class, ex.message
              $stderr.puts ex.backtrace
              
              $logger.error(__FILE__) { __LINE__.to_s + " #{ex.class}: #{ex.message}" }
              
              retryCnt = retryCnt + 1
              retry if retryCnt < 4
            end
          end
        end
      end
      threads
    end
    
  end
  
end
